// pages/list/list.js
import { getViedoList, getNewVideoList, getHotVideoList} from "../../utils/api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:[],
    layerNum: 5,
    pageSize: 10,
    curLoadedPage: 1,
    isMore: true,
    catid:0,
    pageTitle:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  getData: function(videoid,title,stop){
    console.log(videoid, title)
    if (videoid == 8) {
      getNewVideoList(this.data.pageSize, this.data.curLoadedPage, (data) => {
        if (data.length < this.data.pageSize) {
          this.setData({
            isMore: false
          })
        };
        this.setData({
          items: data,
          layerNum: data.length / 2,
          catid: videoid,
          pageTitle: title
        });
        if(stop){
          // 隐藏导航栏加载框
          wx.hideNavigationBarLoading();
          // 停止下拉动作
          wx.stopPullDownRefresh();
        }
      })
    } else if (videoid == 9) {
      getHotVideoList(this.data.pageSize, (data) => {
        if (data.length < this.data.pageSize) {
          this.setData({
            isMore: false
          })
        };
        this.setData({
          items: data,
          layerNum: data.length / 2,
          catid: videoid,
          pageTitle: title
        });
        if (stop) {
          // 隐藏导航栏加载框
          wx.hideNavigationBarLoading();
          // 停止下拉动作
          wx.stopPullDownRefresh();
        }
      })
    } else {
      getViedoList(videoid, this.data.pageSize, this.data.curLoadedPage, (data) => {
        if (data.length < this.data.pageSize) {
          this.setData({
            isMore: false
          })
        };
        this.setData({
          items: data,
          layerNum: data.length / 2,
          catid: videoid,
          pageTitle: title
        });
        if (stop) {
          // 隐藏导航栏加载框
          wx.hideNavigationBarLoading();
          // 停止下拉动作
          wx.stopPullDownRefresh();
        }
      });
    }
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.title
    });
    this.getData(options.catid, options.title);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.setData({
      items: [],
      layerNum: 5,
      pageSize: 10,
      curLoadedPage: 1,
      isMore: true
    })
    this.getData(this.data.catid, this.data.pageTitle, true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中...',
    });
    var temp = this.data.curLoadedPage;
    temp++;
    this.setData({
      curLoadedPage: temp
    })
    // console.log(this.data.catid)
    if(this.data.catid == 8){
      getNewVideoList(this.data.pageSize, this.data.curLoadedPage, (data)=>{
        var temp = this.data.items;
        for (var i = 0; i < data.length; i++) {
          temp.push(data[i]);
        }
        this.setData({
          items: this.data.items,
          layerNum: this.data.items.length / 2
        });
        if (data.length == 0) {
          this.setData({
            isMore: false
          })
        }
        wx.hideLoading();
      })
    }else if(this.data.catid == 9){
      getHotVideoList(this.data.pageSize * this.data.curLoadedPage, (data)=>{
        this.setData({
          items: data,
          layerNum: data / 2
        });
        if (data.length == 0) {
          this.setData({
            isMore: false
          })
        }
        wx.hideLoading();
      })
    }else{
      getViedoList(this.data.catid, this.data.pageSize, this.data.curLoadedPage, (data) => {
        // console.log(data)
        var temp = this.data.items;
        for (var i = 0; i < data.length; i++) {
          temp.push(data[i]);
        }
        this.setData({
          items: this.data.items,
          layerNum: this.data.items.length / 2
        });
        if (data.length == 0) {
          this.setData({
            isMore: false
          })
        }
        wx.hideLoading();
      })
    }
  }
})